package com.guoxw.ibinderdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.guoxw.ibinderdemo.bean.Person;
import com.guoxw.ibinderdemo.server.BookManager;
import com.guoxw.ibinderdemo.server.MyAIDLService;
import com.guoxw.ibinderdemo.server.RemoteService;
import com.guoxw.ibinderdemo.server.Stub;

import java.util.List;

/**
 * client端
 */
public class MainActivity extends AppCompatActivity {

    private Button btn_add,btn2;

    /**
     * 远端连接标识
     */
    private Boolean isConnect = false;

    /**
     * 图书操作类
     */
    private BookManager bookManager;

    private Boolean personIsConnect = false;


    private IMyAidl personManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_add = findViewById(R.id.btn);

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("ClientActivity", "=========点击事件======");
                if(isConnect==false){
                    Log.d("ClientActivity", "=========点击事件==1====");

                    attempToBindService();
                    return;
                }

                if (bookManager == null){
                    Log.d("ClientActivity", "=========点击事件==2====");

                    return;
                }


                Book book = new Book();
                book.setPrice(18);
                book.setName("21天学通c++");
                try {
                    bookManager.addBook(book);

                    Log.d("ClientActivity", bookManager.getBooks().toString());

                } catch (RemoteException e) {
                    Log.d("ClientActivity error", e.toString());

                    e.printStackTrace();
                }

            }
        });

        btn2 = findViewById(R.id.btn2);

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("ClientActivity", "=========点击事件======");
                if(personIsConnect==false){
                    Log.d("ClientActivity", "=========点击事件==1====");

                    attempToBindPersonService();
                    return;
                }

                if (personManager == null){
                    Log.d("ClientActivity", "=========点击事件==2====");

                    return;
                }


                Person guoxw = new Person("guoxw");
                try {
                    personManager.addPerson(guoxw);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void attempToBindService(){
        Log.d("ClientActivity", "=========attempToBindService==1====");

        Intent intent = new Intent(getApplicationContext(), RemoteService.class);
        Log.d("ClientActivity", "=========attempToBindService==2====");
        intent.setAction("com.guoxw.ibinderdemo");
        Log.d("ClientActivity", "=========attempToBindService==3====");
        //启动服务，绑定连接
        boolean bindService = bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        Log.d("ClientActivity", "=========attempToBindService==4===="+bindService);

    }

    private void attempToBindPersonService(){

        Intent intent = new Intent(getApplicationContext(), MyAIDLService.class);
        intent.setAction("com.guoxw.ibinderdemo");
        bindService(intent,personConnection,Context.BIND_AUTO_CREATE);

    }

    /**
     * service链接通道
     */
    private ServiceConnection personConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            personIsConnect = true;
            Log.d("ServiceConnection", "=====personManager====onServiceConnected======");

            personManager = IMyAidl.Stub.asInterface(iBinder);
            if(personManager!=null){

                try {
                    List<Person> personList = personManager.getPersonList();

                    Log.i("=personManager onServiceConnected======",personList.toString());

                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            personIsConnect = false;
        }
    };

    private ServiceConnection serviceConnection = new ServiceConnection(){
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            isConnect = true;
            Log.d("ServiceConnection", "=========onServiceConnected======");

            //连接远端
            bookManager = Stub.asInterface(iBinder);
            if (bookManager != null) {
                try {
                    List<Book> books = bookManager.getBooks();

                } catch (RemoteException e) {
                    Log.d("ServiceConnection error", e.toString());

                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d("ServiceConnection", "=========onServiceDisconnected======");
            isConnect = false;
        }

        @Override
        public void onBindingDied(ComponentName name) {
            ServiceConnection.super.onBindingDied(name);

            Log.d("ServiceConnection", "=========onBindingDied======"+name);

        }

        @Override
        public void onNullBinding(ComponentName name) {
            ServiceConnection.super.onNullBinding(name);

            Log.d("ServiceConnection", "=========onNullBinding======"+name);

        }
    };


    @Override
    protected void onStart() {
        super.onStart();
        if(!isConnect){
            this.attempToBindService();
        }

        if(!personIsConnect){
            this.attempToBindPersonService();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(isConnect){
            //接触service绑定
            unbindService(serviceConnection);
        }
        if(personIsConnect){
            //接触service绑定
            unbindService(personConnection);
        }
    }
}